<template>
  <div class="form-update">
    
    <el-form :model="formObj" class="demo-form" ref="formTab" label-width="100px">
      <el-form-item label="电影标题">
         <el-input v-model="formObj.title" placeholder="电影标题" ></el-input>
      </el-form-item>
      <el-form-item label="主演">
        <el-input v-model="formObj.actors" placeholder="主演" ></el-input>
      </el-form-item>
    
      <el-form-item label="导演">
        <el-input v-model="formObj.director" placeholder="导演" ></el-input>
      </el-form-item>
      <el-form-item label="评分">
        <el-input v-model="formObj.score" placeholder="评分" ></el-input>
      </el-form-item>
      <!-- <el-form-item label="放映时间">
        <el-date-picker
          v-model="formObj.release"
          type="date"
          placeholder="放映时间">
        </el-date-picker>
   
      </el-form-item> -->

      
      <el-form-item label="影片类型">
        <el-checkbox-group  v-model="formObj.movieType">
          <el-checkbox v-for="item in typeList" :key="item._id"  :label="item.id" >{{ item.name }}</el-checkbox>
        </el-checkbox-group>  
      </el-form-item>

      <el-form-item label="语言">
         <el-select v-model="formObj.language" placeholder="语言" >
          <el-option  v-for="item in languageList" :key="item._id" :label="item.language" :value="item.language"></el-option>
         </el-select>  
      </el-form-item>
   
      <el-form-item label="区域/放映时间">
         <ul class="region-box">
            <li v-for="(item,index) in regionData" :key="index"> <el-input  placeholder="所属区域" v-model="item.region" ></el-input> <el-date-picker type="date" v-model="item.release" placeholder="放映时间"> </el-date-picker></li>
         </ul>
      </el-form-item>

      <el-form-item label="时长">
        <el-input v-model="formObj.duration" placeholder="时长" ></el-input>
      </el-form-item>

      <el-form-item label="描述">
        <el-input  type="textarea" v-model="formObj.desc" placeholder="描述" ></el-input>
      </el-form-item>

      <el-form-item label="电影海报">
        <UploadImg @send="(data)=>getImageData(data,'posterSrc')" ></UploadImg>
     
      </el-form-item>
      <el-form-item label="宣传片">
        <UploadImg @send="(data)=>getImageData(data,'trailerLink')" ></UploadImg>
      </el-form-item>

      <el-form-item>
            <el-button type="primary" @click="submitForm('formTab')">提交</el-button>
            <el-button @click="resetForm('formTab')">重置</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
import axiosEl from '@/utils/axios'
import UploadImg from '@/components/UploadImg.vue'
export default {
  data(){
    return {
      releaseArr:[{
        region:'',
        release:''
      }],
      formObj:{
        imgSrc:null,
        posterSrc:null,
        trailerLink:null
      },
      languageList:[],
      regionList:[],
      typeList:[],
      typeList:[],

      headerObj:{
           Authorization:'Bearer '
       },
    }
  },
  components:{
    UploadImg
  },
  methods:{
    // 影片详情
    async getMovieDetail(){
       let resDetail = await  axiosEl.get('/movie/detail/'+this.did);
       
      
       this.formObj = resDetail.data;
       
      },
    getImageData(data,keyname){
      this.formObj[keyname] = data;
    },

    onSuccessFun(response,kind){
      // response.data   图片上传成功后的名字
     this.formObj[kind] = response.data;
    },

   async addMovie(){
     this.formObj.releaseArr = this.releaseArr;
     let resData = await axiosEl({
        method:'post',
        url:'/movie/addinfo',
        data:this.formObj
      })

      if(resData.code==1) {
        console.log(resData);
        
      }
    },
 
    getLanguage(){
      axiosEl.get('/language/all').then((resData)=>{
          if(resData.code==1) {
              this.languageList = resData.data;
          }
       })
    },
    getRegionList(){
      axiosEl.get('/region/all').then((resData)=>{
          if(resData.code==1) {
              this.regionList = resData.data;
          }
       })
    },
    getMovieType(){
      axiosEl.get('/type/all').then((resData)=>{
          if(resData.code==1) {
              this.typeList = resData.data;
          }
       })
    },

    submitForm(){
        this.addMovie();
    }
  },
  mounted(){
    let token = localStorage.getItem('token');
    this.headerObj = {
          Authorization:'Bearer '+token
    }

    this.getLanguage();
    this.getRegionList();
    this.getMovieType();

    this.did = this.$route.query.id;
    this.getMovieDetail();

  }
}
</script>

<style lang="scss">
.form-update {
  // width: 600px;

  input[type="text"] {
    width: 500px;
  }
  .region-box li {
    display: flex;
    margin-bottom: 10px;
      .el-input {
        width: 200px;
        margin-right: 20px;
        input { width:100%; }
    }
  }
}
</style>